﻿using System;

namespace Ipmce.RemoteVoting.Common.Tools.CardReader.ImageParsing
{
	class BdbData : IElement
	{
		protected byte[] Value;

		protected int GetInt(int offset, int size)
		{
			var buf = new byte[size];
			Array.Copy(Value, offset, buf, 0, buf.Length);
			return BigEndianConverter.GetInt(buf);
		}

		protected void SetInt(int offset, int size, int value)
		{
			Array.Copy(BigEndianConverter.GetByteArray(value, size), 0,
			           Value, offset, size);
		}

		protected byte GetByte(int offset)
		{
			return Value[offset];
		}

		protected void SetByte(int offset, byte value)
		{
			Value[offset] = value;
		}

		protected byte[] GetByteArray(int offset, int size)
		{
			var buf = new byte[size];
			Array.Copy(Value, offset, buf, 0, buf.Length);
			return buf;
		}

		protected void SetByteArray(int offset, byte[] value)
		{
			Array.Copy(value, 0, Value, offset, value.Length);
		}

		public int Parse(byte[] data, int offset)
		{
			Array.Copy(data, offset, Value, 0, Value.Length);
			return offset + Value.Length;
		}

		public int Write(byte[] data, int offset)
		{
			Array.Copy(Value, 0, data, offset, Value.Length);
			return offset + Value.Length;
		}

		public int GetSize()
		{
			return Value.Length;
		}

		public BdbData(int size)
		{
			Value = new byte[size];
			Array.Clear(Value, 0, Value.Length);
		}

		public BdbData(byte[] value)
		{
			Value = value;
		}
	}
}